<?php

defined('SYSPATH') or die('No direct access allowed.');

class Kohana_Alipay_Notify extends Alipay {

    /**
     * @string 通知类型
     */
    protected $notify_type = 'notify';
    /**
     * 验证参数
     */
    protected $parameter = array();

    /**
     * 构造方法
     * 
     * @var string 通知类型
     */
    public function __construct($notify) {
        $this->notify_type = $notify;
        $this->parameter = ($notify == 'notify') ? $_POST : $_GET;
        parent::__construct();
    }

    /**
     * 验证通知或返回
     *
     * @return boolean
     */
    public function verify() {
        //需要验证的参数为空，则返回FALSE
        if (empty($this->parameter))
            return FALSE;
        //生成验证地址
        if ($this->transport == "https") {
            $veryfy_url = $this->gateway . "service=notify_verify" .
                    "&partner=" . $this->partner .
                    "&notify_id=" . $this->parameter['notify_id'];
        } else {
            $veryfy_url = $this->gateway . "partner=" . $this->partner .
                    "&notify_id=" . $this->parameter['notify_id'];
        }
        //获取远程服务器ATN结果，验证是否是支付宝服务器发来的请求
        $veryfy_result = $this->get_verify($veryfy_url);
        //对$parameter去空
        $post_or_get = $this->para_filter($this->parameter);
        //对$parameter数据排序
        $sort_post_or_get = $this->arg_sort($this->parameter);
        //生成签名
        $mysign = $this->build_mysign($sort_post_or_get, $this->key,
                        $this->sign_type);

        if (preg_match("/true$/i", $veryfy_result) && $mysign == $this->parameter["sign"])
            return TRUE;
        return FALSE;
    }

    /**
     * 获取远程服务器ATN结果
     *
     * @param string 指定URL路径地址
     * @param int 超时时间
     * 
     * @return 服务器ATN结果集
     */
    protected function get_verify($url, $time_out = 60) {
        $urlarr = parse_url($url);
        $errno = "";
        $errstr = "";
        $transports = "";
        if ($urlarr["scheme"] == "https") {
            $transports = "ssl://";
            $urlarr["port"] = "443";
        } else {
            $transports = "tcp://";
            $urlarr["port"] = "80";
        }
        $fp = @fsockopen($transports . $urlarr['host'], $urlarr['port'], $errno, $errstr, $time_out);
        if (!$fp) {
            die("ERROR: $errno - $errstr<br />\n");
        } else {
            fputs($fp, "POST " . $urlarr["path"] . " HTTP/1.1\r\n");
            fputs($fp, "Host: " . $urlarr["host"] . "\r\n");
            fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            fputs($fp, "Content-length: " . strlen($urlarr["query"]) . "\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $urlarr["query"] . "\r\n\r\n");
            while (!feof($fp)) {
                $info[] = @fgets($fp, 1024);
            }
            fclose($fp);
            $info = implode(",", $info);
            return $info;
        }
    }

}